﻿function pageNavExtend()
{
	var requestCall,showCall,call,array=new Array(),pageSize=0,pageNav,handle,index=1,isEnd=false,isFirst=true;
	
	this.setPageSize=function(s){pageSize=s;}
	this.setPageNav=function(pn){pageNav=pn;}
	this.setHandle=function(h){handle=h;}
	this.setCall=function(c){call=c;}
	this.show=function()
	{
		//show
		var iStart=pageSize*(index-1);
		var iEnd=pageSize*index;
		if(iEnd > array.length){iEnd=array.length;}
		var tempArray=new Array();
		for(var i=iStart;i<iEnd;i++)
		{
			tempArray.push(array[i]);
		}
		call(tempArray);
	}
	this.addArray=function(arr)
	{
		if(arr.length == pageSize)
		{
			if(isFirst)
			{	
				pageNav.addCount(pageSize);
				pageNav.addCount(pageSize);
				isFirst=false;
			}else
			{
				pageNav.addCount(pageSize);
			}
		}else
		{
			isEnd=true;
			pageNav.addCount(arr.length-pageSize);
		}
		for(var i=0;i<arr.length;i++)
		{
			array.push(arr[i]);
		}
		handle.show();
	}
	// 用以请求分页资源
	this.setRequestCall=function(c)
	{
		requestCall=c;
	}
	// 要么请求资源，要么显示资源
	this.setShowCall=function(ind)
	{
		if(index == ind){return;}
		index=ind;
		if(isEnd)
		{
			handle.show();
		}else
		{
			//还没结束，但是往回走
			//alert("(index*pageSize)="+(index*pageSize)+" , array.length="+array.length);
			if((index*pageSize) <= array.length)
			{
				handle.show();
			}else
			{
				requestCall(index);
			}
		}
	}
}

